Reverse Interger
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Example 1:
Input: x = 123
Output: 321
First try
x % 10
didn’t have a solution
Refer to others’ solution
we can use number.toString()
array.split(“”)
array.reverse()
array.join(“”)
Math.pow(2,n) represents 2[^n]
Math.abs()return absolute value
1 | /** |
Runtime: 88 ms, faster than 74.47% of JavaScript online submissions for Reverse Integer.
Memory Usage: 40.3 MB, less than 70.81% of JavaScript online submissions for Reverse Integer.
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !